package Year22_Month06.day19;

public class 二叉搜索树的最小绝对差_方法一 {
    int sum = Integer.MAX_VALUE;//用来保存最小的差值
    int preVal = -100000;//用来保存前一个节点的值
    public int getMinimumDifference(TreeNode root) {
        solve(root);
        return sum;
    }

    public void solve(TreeNode root){
        if(root == null) return;
        if(root.left != null) solve(root.left);
        if(Math.abs(root.val - preVal) < sum){
            sum = Math.abs(root.val - preVal);
        }
        preVal = root.val;
        if(root.right != null) solve(root.right);
    }
}
